<?php
namespace Opencart\Admin\Controller\Extension\Laguageautocomplite\Module;
class Laguageautocomplite extends \Opencart\System\Engine\Controller {



	public function addProduct(&$route, &$data, &$output) { //, &$output
		//$output = str_replace("</head>","<script>alert('ok');</script></head>",$output);
		//echo $output;
		return $output;
    }

	public function createEvents(): void {
        $this->load->model('setting/event');

        foreach ($this->events as $trigger => $actions) {
            foreach ($actions as $action) {
				if( !$this->model_setting_event->getEventByCode('laguage_autocomplite') ){
					$this->model_setting_event->addEvent(
						[
							'code'=>'laguage_autocomplite',
							'description'=>'Auto Ijection Autocomplite Language Dublicate',
							'trigger'=>$trigger,
							'action'=>$action,
							'status'=>1,
							'sort_order'=>0
						]
					);
				}
            }
        }
    }

	public function deleteEvents(): void {
		$this->model_setting_event->deleteEventByCode('laguage_autocomplite');
	}

	public function index(): void {
		$this->load->language('extension/laguageautocomplite/module/laguageautocomplite');

		//$this->createEvents();
		//$this->load->model('setting/event');
		//$this->model_setting_event->addEvent($this->events);
 
		$this->document->setTitle($this->language->get('heading_title'));

		$data['breadcrumbs'] = [];

		$data['breadcrumbs'][] = [
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
		];

		$data['breadcrumbs'][] = [
			'text' => $this->language->get('text_extension'),
			'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module')
		];

		$data['breadcrumbs'][] = [
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('extension/laguageautocomplite/module/laguageautocomplite', 'user_token=' . $this->session->data['user_token'])
		];

		$data['save'] = $this->url->link('extension/laguageautocomplite/module/laguageautocomplite|save', 'user_token=' . $this->session->data['user_token']);
		$data['back'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module');

		$data['module_laguageautocomplite_response'] = $this->config->get('module_laguageautocomplite_response');

		$data['module_laguageautocomplite_approved_status_id'] = $this->config->get('module_laguageautocomplite_approved_status_id');
		$data['module_laguageautocomplite_failed_status_id'] = $this->config->get('module_laguageautocomplite_failed_status_id');
		$data['module_laguageautocomplite_order_status_id'] = $this->config->get('module_laguageautocomplite_order_status_id');

		$data['module_laguageautocomplite_public_key'] = $this->config->get('module_laguageautocomplite_public_key');
		$data['module_laguageautocomplite_private_key'] = $this->config->get('module_laguageautocomplite_private_key');

		$this->load->model('localisation/order_status');

		$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

		$data['module_laguageautocomplite_geo_zone_id'] = $this->config->get('module_laguageautocomplite_geo_zone_id');

		$data['module_laguageautocomplite_minimum_total'] = $this->config->get('module_laguageautocomplite_minimum_total');

		$data['module_laguageautocomplite_server_prefix'] = $this->config->get('module_laguageautocomplite_server_prefix');
  //http://127.0.0.1/opencart/
		$this->load->model('localisation/geo_zone');

		$data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

		$data['module_laguageautocomplite_status'] = $this->config->get('module_laguageautocomplite_status');
		$data['module_laguageautocomplite_sort_order'] = $this->config->get('module_laguageautocomplite_sort_order');

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('extension/laguageautocomplite/module/laguageautocomplite', $data));
	}

	public function save(): void {
		$this->load->language('extension/laguageautocomplite/module/laguageautocomplite');

		if( $this->request->post['module_laguageautocomplite_status'] == 1 ){
			$this->createEvents();
		} else {
			$this->deleteEvents();
		}

		$json = [];

		if (!$this->user->hasPermission('modify', 'extension/laguageautocomplite/module/laguageautocomplite')) {
			$json['error'] = $this->language->get('error_permission');
		}

		if (!$json) {
			$this->load->model('setting/setting');

			$this->model_setting_setting->editSetting('module_laguageautocomplite', $this->request->post);

			$json['success'] = $this->language->get('text_success');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function inject_abc_javascript($route, &$data = false, &$output = false){
		// Before closing tag
		$hook = '</head>';
		$js = '<script>alert("Hello from one_two_three module!");</script></head>';
		$output = str_replace($hook, $js, $output);
	}


}
